package com.glasses.business.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.activity.BackMoneyActivity;
import com.glasses.business.activity.OrderCompanyActivity;
import com.glasses.business.activity.PayActivity;
import com.glasses.business.base.MyBaseAdapter;
import com.glasses.business.entity.OrderList;
import com.glasses.business.entity.PayInfo;
import com.glasses.business.entity.User;
import com.glasses.business.event.BussEvent;
import com.glasses.business.fragment.PayFragment;
import com.glasses.business.http.NetUtils;
import com.glasses.business.inter.OnItemClick;
import com.glasses.business.utils.Tools;
import com.jcodecraeer.xrecyclerview.ProgressStyle;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.ToastUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/6
 * Version：1.0
 */

public class OrderAdapter extends MyBaseAdapter<OrderList, OrderAdapter.ViewHolder> {


    public OrderAdapter(Activity context, List data) {
        super(context, data);
    }

    @Override
    protected ViewHolder getViewHolder() {
        return new ViewHolder(mInflater.inflate(R.layout.item_order, null));
    }

    @Override
    protected void onBindViewHolder_(ViewHolder holder, int position) {
        if (data != null && data.size() > 0) {
            final OrderList orderList = data.get(position);
            holder.totalTv.setText("共" + orderList.getProductCount() + "件商品  合计：" + orderList.getFinalPrice());
            switch (orderList.getStatus()) {
                case 1:
                    holder.orderStatus.setText("待付款");
                    holder.cancelTv.setVisibility(View.VISIBLE);
                    holder.payTv.setVisibility(View.VISIBLE);
                    holder.cancelTv.setText("取消订单");
                    holder.payTv.setText("付款");
                    holder.cancelTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            cancelDel(0, orderList);
                        }
                    });
                    holder.payTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            pay(orderList);
                        }
                    });
                    break;
                case 2:
                    holder.orderStatus.setText("待收货");
                    holder.cancelTv.setVisibility(View.GONE);
                    holder.payTv.setVisibility(View.VISIBLE);
                    holder.payTv.setText("确认收货");
                    holder.payTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            sureReceiver(orderList);
                        }
                    });
                    break;
                case 3:
                    holder.orderStatus.setText("已完成");
                    holder.cancelTv.setVisibility(View.VISIBLE);
                    holder.payTv.setVisibility(View.VISIBLE);
                    holder.cancelTv.setText("申请售后");
                    holder.payTv.setText("删除订单");
                    holder.cancelTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (System.currentTimeMillis() < orderList.getCanApplyRefendTime()) {
                                cancelDel(1, orderList);
                            } else {
                                ToastUtil.shortShowToast("退货时间已过...");
                            }
                        }
                    });
                    holder.payTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            cancelDel(2, orderList);
                        }
                    });
                    break;
                case 4:
                    holder.orderStatus.setText("售后中");
                    holder.cancelTv.setVisibility(View.GONE);
                    holder.payTv.setVisibility(View.GONE);
                    break;
                case 5:
                    holder.orderStatus.setText("售后完成");
                    holder.cancelTv.setVisibility(View.GONE);
                    holder.payTv.setVisibility(View.VISIBLE);
                    holder.payTv.setText("删除订单");
                    holder.payTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            cancelDel(2, orderList);
                        }
                    });
                    break;
            }
            StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,
                    StaggeredGridLayoutManager.VERTICAL);
            layoutManager.setOrientation(StaggeredGridLayoutManager.VERTICAL);
            holder.goodsRecylerView.setLayoutManager(layoutManager);
            OrderGoodsAdapter adapter = new OrderGoodsAdapter(context, null);
            holder.goodsRecylerView.setAdapter(adapter);
            holder.goodsRecylerView.addItemDecoration(new SpaceItemDecoration(2));
            adapter.appendAll(orderList.getProducts());
            adapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, List data, int position) {
                    onItemClick.onItemClick(0, orderList);
                }
            });
            holder.orderLL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClick.onItemClick(0, orderList);
                }
            });
        }
    }

    private void pay(OrderList orderList) {
        Tools.showDialog(context);
        NetUtils.getInstance().payInformation(orderList.getId(), new NetCallBack() {
            @Override
            public void onSuccess(String response, String mssage, ResultModel model) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
                PayInfo payInfo = (PayInfo) model.getModel();
                switch (payInfo.getIsHaveProduct0Information()) {
                    case 0:
                        Intent intent1 = new Intent(context, OrderCompanyActivity.class);
                        intent1.putExtra("payMoney", payInfo);
                        context.startActivity(intent1);
                        break;
                    case 1:
                        Intent intent = new Intent(context, PayActivity.class);
                        intent.putExtra("payMoney", payInfo);
                        context.startActivity(intent);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
            }
        }, PayInfo.class);
    }

    private void cancelDel(final int flag, final OrderList orderList) {
        String title = null;
        switch (flag) {
            case 0:
                title = "确认取消该订单?";
                break;
            case 1:
                title = "确认售后?";
                break;
            case 2:
                title = "确认删除该订单?";
                break;
        }
        new AlertDialog.Builder(context).setMessage(title)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        switch (flag) {
                            case 0://取消订单
                                cancelOrder(orderList);
                                break;
                            case 1://售后
                                Intent intent = new Intent(context, BackMoneyActivity.class);
                                intent.putExtra("id", orderList.getId());
                                context.startActivity(intent);
                                break;
                            case 2://删除订单
                                cancelOrder(orderList);
                                break;
                        }
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }).create().show();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.orderLL)
        LinearLayout orderLL;
        @InjectView(R.id.orderStatus)
        TextView orderStatus;
        @InjectView(R.id.goodsRecylerView)
        RecyclerView goodsRecylerView;
        @InjectView(R.id.totalTv)
        TextView totalTv;
        @InjectView(R.id.cancelTv)
        TextView cancelTv;
        @InjectView(R.id.payTv)
        TextView payTv;

        ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

            if (parent.getChildPosition(view) != 0) {
                outRect.bottom = space;
                outRect.top = space;
            }
        }
    }

    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    private void sureReceiver(OrderList orderList) {
        Tools.showDialog(context);
        NetUtils.getInstance().sureReceive(orderList.getId(), new NetCallBack() {
            @Override
            public void onSuccess(String response, String mssage, ResultModel model) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
                EventBus.getDefault().post(new BussEvent(BussEvent.Order_Refush));
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
            }
        }, null);
    }

    private void cancelOrder(OrderList orderList) {
        Tools.showDialog(context);
        NetUtils.getInstance().cancelOrder(orderList.getId(), new NetCallBack() {
            @Override
            public void onSuccess(String response, String mssage, ResultModel model) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
                EventBus.getDefault().post(new BussEvent(BussEvent.Order_Refush));
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
            }
        }, null);
    }
}
